﻿using System;
using DDU.Infrastructure;
using DDU.Enums;
using System.Data.Linq.Mapping;
using System.Reflection;

namespace DDU.DataModel
{
    [Table(Name = "T_Semester")]
    public class SemesterInfo : NotificationObject
    {
        private string _guid = System.Guid.NewGuid().ToString();
        private EducationLevelValue _eduLevel = EducationLevelValue.University;
        private int _grade = 1;
        private HalfTermValue _term = HalfTermValue.Spring;
        private DateTime _startTime = DateTime.Now;
        private DateTime _endTime = DateTime.Now;

        [Column(IsPrimaryKey=true)]
        public string Guid
        {
            get { return _guid; }
            set
            {
                _guid = value;
                RaisePropertyChanged(() => Guid);
            }
        }

        [Column]
        public EducationLevelValue EducationLevel
        {
            get { return _eduLevel; }
            set
            {
                _eduLevel = value;
                RaisePropertyChanged(() => EducationLevel);
            }
        }

        [Column]
        public int Grade
        {
            get { return _grade; }
            set
            {
                _grade = value;
                RaisePropertyChanged(() => Grade);
            }
        }

        [Column]
        public HalfTermValue Term
        {
            get { return _term; }
            set
            {
                _term = value;
                RaisePropertyChanged(() => Term);
            }
        }

        [Column]
        public DateTime StartTime
        {
            get { return _startTime; }
            set
            {
                _startTime = value;
                RaisePropertyChanged(() => StartTime);
            }
        }

        [Column]
        public DateTime EndTime
        {
            get { return _endTime; }
            set
            {
                _endTime = value;
                RaisePropertyChanged(() => EndTime);
            }
        }

        public int Year
        {
            get
            {
                if (StartTime != null)
                {
                    return StartTime.Year;
                }
                return DateTime.Now.Year;
            }
        }

        public event EventHandler<PropertyArgs> ValueChanged = delegate { };

        protected override void RaisePropertyChanged(string propertyName)
        {
            base.RaisePropertyChanged(propertyName);
            ValueChanged.Invoke(this, new PropertyArgs()
            {
                Property = GetType().GetProperty(propertyName)
            });
        }
    }
}
